package megrez.job.custom;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import megrez.job.custom.web.vo.ConfigInput;

/**
 * 工具
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Component
public class Utils {
    @Autowired
    private ObjectMapper objectMapper;
    
    public ConfigInput createWithParamsConfig() {
        return new ConfigInput("megrez.job.custom.tasks.WithParamsTestTask", "withParams", "abc",
                        "0/1 * * * * ?", "带参数任务", false);
    }


    public ConfigInput createNoParamsConfig() {
        return new ConfigInput("megrez.job.custom.tasks.NoParamTestTask", "noParams", null,
                        "0/2 * * * * ?", "不带参数任务", true);
    }

    public ConfigInput createExceptionConfig() {
        return new ConfigInput("megrez.job.custom.tasks.ExceptionTestTask", "execute", null,
                        "0/2 * * * * ?", "异常任务", true);
    }


    public <T> T paseObject(String value, TypeReference<T> valueTypeRef) {
        try {
            return objectMapper.readValue(value, valueTypeRef);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    public <T> T paseObject(String value, Class<T> clazz) {
        try {
            return objectMapper.readValue(value, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    public String asJsonString(Object o) {
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
